package com.aaa.car.controller;


import com.aaa.car.service.CarService;
import com.aaa.common.entity.Car;
import com.aaa.common.entity.ResultData;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ypy
 * @since 2020-07-300..0
 */
@Controller
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CarService carService;

    @ResponseBody
    @GetMapping("/getAllCar")
    public ResultData getAllCar(){

        List<Car> car=carService.selectList(null);
     //   PageInfo pageInfo = carService.getAllCar(pageSize,pageNum);
        if (car!=null){
            return new ResultData(true,"查询成功",car );
        }else {
            return new ResultData(false,"查询失败",null);
        }
    }


    @ResponseBody
    @GetMapping("/getCar")
    public ResultData getCar( String caetype,Integer carnumber){
        System.out.println(caetype);
        System.out.println(carnumber);
        Wrapper wrapper = new EntityWrapper();
        if (null!=carnumber&&!"".equals(carnumber)){
            String carNum= String.valueOf(carnumber);
            wrapper.like("CARNUMBER",carNum);
            //wrapper.eq("CARNUMBER",carnumber);
        }
        if (null!=caetype&&!"".equals(caetype)){
            //  wrapper.like()
            wrapper.like("CAETYPE",caetype);
        }
        List<Car> car = carService.selectList(wrapper);
        System.out.println(car);
        if (car!=null){
            return new ResultData(true,"查询成功",car);
        }else {
            return new ResultData(false,"查询失败",null);
        }
    }

    @RequestMapping("/addCar")
    public ResultData addCar(@RequestBody Car car){
        System.out.println(car);

        boolean insert=carService.insert(car);
        if (insert){
            return new ResultData(true,"新增成功",null);
        }else {
            return new ResultData(false,"新增失败",null);
        }
    }

    @RequestMapping("/updateCar")
    public ResultData updateCar(@RequestBody Car car){
        System.out.println(car);
        boolean update=carService.updateById(car);
        if (update){
            return new ResultData(true,"修改成功",null);
        }else {
            return new ResultData(false,"修改失败",null);
        }
    }

    @RequestMapping("/deleteCar")
    public ResultData deleteCar(Integer CARID){
        boolean update=carService.deleteById(CARID);
        if (update){
            return new ResultData(true,"修改成功",null);
        }else {
            return new ResultData(false,"修改失败",null);
        }
    }
}

